<?php
/**
 * 后台首页
 * 
 * @author renpper@qq.com
 * @date 2014-12-8
 */
namespace Admin\Controller;

class HomeController extends BaseController {
   
	public function index(){
    	$this->assign('menus', $this->getMenuTree());
    	$this->assign('info', $this->getServerInfo());
    	$this->display('Admin/View/Home/index.php');
    }
    
    
    /**
     * 获得php配置选项 
     */
    protected function getCfg($option_name){
    	$result = get_cfg_var($option_name);
    	if($result == 0){
    		return 'No';
    	}elseif($result == 1){
    		return 'Yes';
    	}else{
    		return $result;	
    	}
    }
    
    /**
     * 获得服务器信息
     */
    protected function getServerInfo(){
    	$dis_func = get_cfg_var('disable_functions');
    	$upsize= $this->getCfg('file_uploads') ? $this->getCfg('upload_max_filesize') : 'Not allowed';
		$adminmail=isset($_SERVER['SERVER_ADMIN']) ? $_SERVER['SERVER_ADMIN'] : $this->getCfg('sendmail_from');
		!$dis_func && $dis_func = 'No';	
		$info = array(
			array('服务器时间',date('Y-m-d h:i:s')),
			array('服务器主机',$_SERVER['SERVER_NAME']),
			array('服务器IP',gethostbyname($_SERVER['SERVER_NAME'])),
			array('操作系统',PHP_OS),
			//array('Server OS Charset',$_SERVER['HTTP_ACCEPT_LANGUAGE']),
			array('服务器软件',$_SERVER['SERVER_SOFTWARE']),
			array('服务器端口',$_SERVER['SERVER_PORT']),
			array('PHP运行模式',strtoupper(php_sapi_name())),
			//array('The file path',__FILE__),
	
			array('PHP版本',PHP_VERSION),
			array('PHP信息',('<a href="'.__WWWROOT__.'Admin/System/phpInfo.php" target="_blank" style="text-decoration:underline;color:blue" >Yes</a>')),
			array('安全模式',$this->getCfg('safe_mode')),
			array('管理员',$adminmail),
			//array('allow_url_fopen',$this->getCfg('allow_url_fopen')),
			//array('enable_dl',$this->getCfg('enable_dl')),
			array('显示错误',$this->getCfg('display_errors')),
			array('register_globals',$this->getCfg('register_globals')),
			array('magic_quotes_gpc',$this->getCfg('magic_quotes_gpc')),
			array('memory_limit',$this->getCfg('memory_limit')),
			array('post_max_size',$this->getCfg('post_max_size')),
			array('upload_max_filesize',$upsize),
			//array('max_execution_time',$this->getCfg('max_execution_time').' second(s)'),
			//array('disable_functions', $dis_func)
    	);
    	return $info;
    }

	/**
	 * 获得菜单树
	 */
	//protected function getMenuTree(){
	public function getMenuTree(){
		$tree = '';
		
		if(isset($_SESSION['SESSION_USER']['menu_tree'])){
			return $_SESSION['SESSION_USER']['menu_tree'];
		}
		
		$role_id = (int)$_SESSION['SESSION_USER']['role_id'];
		$role = getInstance('Admin\Model\RoleModel')->field('menu_ids')->where("id=$role_id")->find();
		$menu_ids = $role['menu_ids'];
		$menu_model = getInstance('Admin\Model\MenuModel');
		if($menu_ids){
			$menus = $menu_model->getParentMenu(explode(',', $menu_ids));
		}
		$_SESSION['SESSION_USER']['role_menus'] = $menus;
		$menus = $menu_model->buildTree($menus, 0, 1);
		
		function getChildNode($childs){
			if($childs == null){
				return null;
			}
			$tree ='';
			foreach($childs as $child){
				$tree .= '';
				$url = $child['url'];
				if($url){
					$urls = explode('/', $url);
					$rel = strtolower($urls[0]);
					$href = (strpos($url, 'http://') !== false) ? "href=$url" : "href=".__WWWROOT__.'Admin/'.$url;
				}else{
					$href = 'href="javascript:"';
				}
				if(isset($child['childs'])){
					$tree .= "<li><a>{$child['name']}</a><ul>";
					$tree .= getChildNode($child['childs']);
					$tree .= '</ul></li>';
				}else{
					if($child['level'] == 0){
						$target = $child['target'];
						$tree .= "<li><a $href target=\"{$target}\" rel=\"$rel\" ".(($target == 'dialog') ? " width='{$child['width']}' height='{$child['height']}' " : '').">{$child['name']}</a></li>";
					}
						
				} 
			}
			return $tree;
		}
		
		if(is_array($menus)){
			foreach($menus as $menu){
				$tree .= '<div class="accordionHeader"><h2><span>Folder</span>'.$menu['name'].'</h2></div><div class="accordionContent"><ul class="tree treeFolder">';
				$tree .= getChildNode($menu['childs']);
				$tree .= '</ul></div>';
			}
		}
		$_SESSION['SESSION_USER']['menu_tree'] = $tree;
		return $tree;
	}
}
?>